package org.garbagecan.xstreamstudy.userconf;

import com.thoughtworks.xstream.XStream;

public class Test {
	public static void main(String[] args) throws Exception {
		//testUserConfs();
		testViewPreference();
	}
	
	public static void testUserConfs() {
		XStream xstream = new XStream();
		xstream.alias("UserConfs", UserConfs.class);
		
		xstream.alias("ViewPreference", ViewPreference.class);
		xstream.aliasAttribute("Type", "type");
		xstream.aliasAttribute("EnableAutoRefresh", "enableAutoRefresh");
		xstream.aliasAttribute("MaxPageSize", "maxPageSize");
		xstream.aliasAttribute("RefreshTime", "refreshTime");
		xstream.aliasAttribute("SelectedColumns", "selectedColumns");
		xstream.aliasAttribute("UnselectedColumns", "unselectedColumns");
		xstream.aliasAttribute("SortBy", "sortBy");
		xstream.aliasAttribute("SortDir", "SsortDir");
		xstream.aliasAttribute("TitleName", "titleName");
		xstream.useAttributeFor(ViewPreference.class, "type");
		xstream.useAttributeFor(ViewPreference.class, "enableAutoRefresh");
		xstream.useAttributeFor(ViewPreference.class, "maxPageSize");
		xstream.useAttributeFor(ViewPreference.class, "refreshTime");
		xstream.useAttributeFor(ViewPreference.class, "selectedColumns");
		xstream.useAttributeFor(ViewPreference.class, "unselectedColumns");
		xstream.useAttributeFor(ViewPreference.class, "sortBy");
		xstream.useAttributeFor(ViewPreference.class, "sortDir");
		xstream.useAttributeFor(ViewPreference.class, "titleName");
		
		xstream.alias("FilterSetting", FilterSetting.class);
		xstream.aliasAttribute("Type", "type");
		xstream.aliasAttribute("Name", "name");
		xstream.aliasAttribute("Value", "value");
		xstream.useAttributeFor(FilterSetting.class, "type");
		xstream.useAttributeFor(FilterSetting.class, "name");
		xstream.useAttributeFor(FilterSetting.class, "value");
		
		UserConfs userConfs = new UserConfs();
		
		ViewPreference viewPreference1 = new ViewPreference();
		viewPreference1.setType("host");
		viewPreference1.setEnableAutoRefresh(true);
		viewPreference1.setMaxPageSize(10);
		viewPreference1.setRefreshTime(60);
		viewPreference1.setSelectedColumns("");
		viewPreference1.setUnselectedColumns("");
		viewPreference1.setSortBy("");
		viewPreference1.setSortDir("");
		//viewPreference1.setTitleName("");
		
		ViewPreference viewPreference2 = new ViewPreference();
		viewPreference2.setType("consumer");
		viewPreference2.setEnableAutoRefresh(false);
		viewPreference2.setMaxPageSize(10);
		viewPreference2.setRefreshTime(60);
		viewPreference2.setSelectedColumns("");
		viewPreference2.setUnselectedColumns("");
		viewPreference2.setSortBy("");
		viewPreference2.setSortDir("");
		//viewPreference2.setTitleName("");
		
		userConfs.getViewPreferences().add(viewPreference1);
		userConfs.getViewPreferences().add(viewPreference2);
		
		FilterSetting filterSetting1 = new FilterSetting();
		filterSetting1.setType("host");
		filterSetting1.setName("HostState");
		filterSetting1.setValue("ok");
		
		FilterSetting filterSetting2 = new FilterSetting();
		filterSetting2.setType("host");
		filterSetting2.setName("HostType");
		filterSetting2.setValue("Any");
		
		userConfs.getFilterSettings().add(filterSetting1);
		userConfs.getFilterSettings().add(filterSetting2);
		
		xstream.addImplicitCollection(UserConfs.class, "viewPreferences");
		xstream.addImplicitCollection(UserConfs.class, "filterSettings");
		
		String xml = xstream.toXML(userConfs);
		System.out.println(xml);
		
		userConfs = (UserConfs)xstream.fromXML(xml);
		System.out.println(userConfs);
	}
	
	public static void testViewPreference() {
		XStream xstream = new XStream();
		xstream.registerConverter(new ViewPreferenceConverter());
		
		xstream.alias("ViewPreference", ViewPreference.class);
		
		ViewPreference viewPreference = new ViewPreference();
		viewPreference.setType("host");
		viewPreference.setEnableAutoRefresh(true);
		viewPreference.setMaxPageSize(10);
		viewPreference.setRefreshTime(60);
		viewPreference.setSelectedColumns("");
		viewPreference.setUnselectedColumns("");
		viewPreference.setSortBy("");
		viewPreference.setSortDir("");
		viewPreference.setTitleName("");
		
		xstream.addImplicitCollection(ViewPreference.class, "viewPreferences");
		
		String xml = xstream.toXML(viewPreference);
		System.out.println(xml);
		
		viewPreference = (ViewPreference)xstream.fromXML(xml);
		System.out.println(viewPreference);
	}
}
